package com.web.web.filter;

import com.web.farme.tool.BeanRegistryUtils;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;


public class RedisSessionFilterRegistrar implements ImportBeanDefinitionRegistrar {


    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        BeanDefinitionBuilder redisSessionFilterBean = BeanDefinitionBuilder.rootBeanDefinition(RedisSessionFilter.class);
        BeanRegistryUtils.registerBeanDefinition(registry, redisSessionFilterBean.getBeanDefinition(), "redisSessionFilterBean");
        BeanDefinitionBuilder allowOriginFilterBean = BeanDefinitionBuilder.rootBeanDefinition(AllowOriginFilter.class);
        BeanRegistryUtils.registerBeanDefinition(registry, allowOriginFilterBean.getBeanDefinition(), "allowOriginFilterBean");

        BeanDefinitionBuilder filterRegistrationBean = BeanDefinitionBuilder.rootBeanDefinition(FilterRegistrationBean.class);
        filterRegistrationBean.addPropertyReference("filter", "redisSessionFilterBean");
        filterRegistrationBean.addPropertyValue("urlPatterns", "/*");
        filterRegistrationBean.addPropertyValue("name", "RedisSessionFilter");
        filterRegistrationBean.addPropertyValue("order", 1);
        BeanRegistryUtils.registerBeanDefinition(registry, filterRegistrationBean.getBeanDefinition(), "redisSessionFilter");
        BeanDefinitionBuilder filterRegistrationBean1 = BeanDefinitionBuilder.rootBeanDefinition(FilterRegistrationBean.class);
        filterRegistrationBean1.addPropertyReference("filter", "allowOriginFilterBean");
        filterRegistrationBean1.addPropertyValue("urlPatterns", "/*");
        filterRegistrationBean1.addPropertyValue("name", "AllowOriginFilter");
        filterRegistrationBean1.addPropertyValue("order", 1);
        BeanRegistryUtils.registerBeanDefinition(registry, filterRegistrationBean1.getBeanDefinition(), "allowOriginFilter");
    }
}
